package com.example.treeHole.service.impl;

import com.example.treeHole.dao.CollectMainMapper;
import com.example.treeHole.dao.NotificationMainMapper;
import com.example.treeHole.service.CollectMainService;
import com.example.treeHole.utils.BaseContext;
import com.example.treeHole.utils.Uuid;
import com.example.treeHole.vo.ArticleLike;
import com.example.treeHole.vo.CollectMain;
import com.example.treeHole.vo.NotificationMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CollectMainServiceImpl implements CollectMainService {

    @Autowired
    private CollectMainMapper collectMainMapper;

    @Autowired
    private NotificationMainMapper notificationMainMapper;
    @Override
    public void like(String articleId) {
        String uId = BaseContext.getCurrentId();
        //先根据articleId和uId查询数据库 有则删除没有则添加
        int count=collectMainMapper.collect(articleId,uId);
        //不等于0则表示已经点赞了
        if(count!=0){
            collectMainMapper.deleteCollect(articleId,uId);
        }else {
            CollectMain collectMain=new CollectMain();
            collectMain.setId(Uuid.getUuID());
            collectMain.setArticleId(articleId);
            collectMain.setCreateBy(uId);
            collectMain.setCreateDate(new Date());
            collectMainMapper.insert(collectMain);
            //todo 消息通知
            //点赞后通知文章本人，谁点赞了。
            NotificationMain notificationMain =new NotificationMain();
            notificationMain.setId(Uuid.getUuID());
            notificationMain.setArticleId(articleId);
            notificationMain.setSendId(BaseContext.getCurrentId());
            notificationMain.setCreateBy(BaseContext.getCurrentId());
            notificationMain.setCreateDate(new Date());
            //未处理
            notificationMain.setNotificationStatus("0");
            //"4"表示收藏
            notificationMain.setNotificationCategory("4");
            notificationMain.setNotificationType("1");
            notificationMainMapper.insert(notificationMain);
        }
    }
}
